package com.example.huawei.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.huawei.system.entity.CarInfo;
import com.example.huawei.system.entity.CarouselImage;
import com.example.huawei.system.result.Result;
import com.example.huawei.system.service.CarInfoService;
import com.example.huawei.system.service.CarouselImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "轮播图接口")
@RestController
@RequestMapping("/carouselImage")
public class CarouselImageController {
    @Autowired
    private CarouselImageService carouselImageService;

//    @ApiOperation(value = "分页查询轮播图信息", notes = "分页查轮播图信息")
//    @GetMapping("/page")
//    public Result<IPage<CarouselImage>> queryPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
//                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
//        Page<CarouselImage> page = new Page<>(pageNo, pageSize);
//        return Result.ok(carouselImageService.page(page));
//    }

    @ApiOperation(value = "查询轮播图数据", notes = "查询轮播图数据")
    @GetMapping("/list")
    public Result<List<CarouselImage>> queryList(CarouselImage carouselImage) {

        return Result.ok(       carouselImageService.list(
                new LambdaQueryWrapper<CarouselImage>()
                        .eq(StringUtils.isNotBlank(carouselImage.getType()),CarouselImage::getType, carouselImage.getType())
        ));
    }


    @ApiOperation(value = "新增轮播图信息", notes = "新增轮播图信息")
    @PostMapping("/add")
    public Result<?> add(@RequestBody CarouselImage carouselImage) {
        carouselImage.setCreateTime(new Date());
        boolean flag = carouselImageService.save(carouselImage);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "修改轮播图信息", notes = "修改轮播图信息")
    @PutMapping("/edit")
    public Result<?> edit(@RequestBody CarouselImage carouselImage) {
        boolean flag = carouselImageService.updateById(carouselImage);
        return flag ? Result.ok() : Result.error();
    }

    @ApiOperation(value = "删除轮播图信息", notes = "删除轮播图信息")
    @DeleteMapping("/del/{id}")
    public Result<?> delete(@PathVariable("id") String id) {
        boolean flag = carouselImageService.removeById(id);
        return flag ? Result.ok() : Result.error();
    }
}
